﻿using System.Collections.Generic;

namespace Intyme.Common.DataModel {
    public class DataModel {
        public OrdersRepository OrdersRepository { get; private set; }
        public ComponentsRepository Components { get; private set; }
        public MachinesRepository Machines { get; private set; }
        public ScheduledMachines ScheduledMachines { get; private set; }


        // dictionary MachineID,index;
        public Dictionary<int, int> WorkCastingMachines;
        // dictionary index,MachineID;
        public Dictionary<int, int> WorkCastingMachinesReverse;

        // dictionary MachineID,index;
        public Dictionary<int, int> WorkCNC_ManualMachines;
        // dictionary index,MachineID;
        public Dictionary<int, int> WorkCNC_ManualMachinesReverse;

        // dictionary MachineID,index;
        public Dictionary<int, int> ManualWorkstations;
        // dictionary index,MachineID;
        public Dictionary<int, int> ManualWorkstationsReverse;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkCastingOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkCastingOrders;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkCNC_ManualOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkCNC_ManualOrders;

        // dictionary index,OperationID;
        public Dictionary<int, int> WorkManualOperations;
        // dictionary index, OrderID
        public Dictionary<int, int> WorkManualOrders;

        public DataModel() {
            OrdersRepository = new OrdersRepository();
            Components = new ComponentsRepository();
            Machines = new MachinesRepository();
            ScheduledMachines = new ScheduledMachines();

            WorkCastingMachines = new Dictionary<int, int>();
            WorkCastingMachinesReverse = new Dictionary<int, int>();
            WorkCNC_ManualMachines = new Dictionary<int, int>();
            WorkCNC_ManualMachinesReverse = new Dictionary<int, int>();
            ManualWorkstations = new Dictionary<int, int>();
            ManualWorkstationsReverse = new Dictionary<int, int>();

            WorkCastingOperations = new Dictionary<int, int>();
            WorkCastingOrders = new Dictionary<int, int>();
            WorkCNC_ManualOperations = new Dictionary<int, int>();
            WorkCNC_ManualOrders = new Dictionary<int, int>();
            WorkManualOperations = new Dictionary<int, int>();
            WorkManualOrders = new Dictionary<int, int>();
        }

        public Operation GetOperationByID(int id, ComponentsRepository componentsRepository) {
            foreach (var component in componentsRepository.Components) {
                foreach (var operationPair in component.Value.Operations) {
                    if (operationPair.Key == id)
                        return operationPair.Value;
                }

                //Operation Operaion = GetOperationByID(ID, aComponent.Value. 
            }


            // TODO: get rid of 'null' return
            return null;
        }

        //public Component GetComponentByOperationID(int ID, ComponentsRepository Components)
        //{
        //    foreach (KeyValuePair<int, Component> aComponent in Components.Components)
        //    {
        //        foreach (KeyValuePair<int, Operation> operation_pair in aComponent.Value.Operations)
        //        {
        //            if (operation_pair.Key == ID)
        //                return aComponent.Value;
        //        }

        //        //Operation Operaion = GetOperationByID(ID, aComponent.Value. 
        //    }

        //    return null;
        //}

        public void DeleteAllData() {
            OrdersRepository.Clear();
            Components.Clear();
            Machines.Clear();
            ScheduledMachines.Clear();
        }

        public string AddComponent(Component newComponent) {
            if (Components != null) {
                Components.AddComponent(newComponent);
            }

            return string.Empty;
        }

        public string AddMachine(Machine newMachine) {
            if (Machines != null) {
                Machines.AddMachine(newMachine);
            }

            return string.Empty;
        }

        public string AddOrder(Order newOrder) {
            if (OrdersRepository != null) {
                OrdersRepository.AddOrder(newOrder);
            }

            return string.Empty;
        }
    }
}